﻿using System;
using EzDesk.DesktopClient.Command;
using EzDesk.DesktopClient.FunctionWindowModule.ViewContract;
using EzDesk.DesktopClient.MVVMEnhance;
using EzDesk.Generic;

namespace EzDesk.DesktopClient.FunctionWindowModule.ViewModel
{
    public interface IFunctionPageViewModel
    {
        string Url { get; set; }
        void Navigate(string url);
        IFunctionPageView View { get; set; }
        DelegateCommand CloseCommand { get; set; }
        event EventHandler OnCloseViewModel;
        string CallName { get; set; }
        IFunctionPageViewModel SetCallName(string callName);
    }

    public class FunctionPageViewModel : ViewModelWithSingleContainer<IFunctionPageView>, IFunctionPageViewModel
    {
        private string _url;

        public string Url
        {
            get { return _url; }
            set
            {
                _url = value;
                RaisePropertyChanged("Url");
                this.View.Navigate(_url);
            }
        }

        public string CallName { get; set; }
        

        public FunctionPageViewModel()
            : base()
        {
            CloseCommand = new DelegateCommand(this.OnClose);
        }

        public IFunctionPageViewModel SetCallName(string callName)
        {
            this.CallName = callName;
            return this;
        }

        private void OnClose()
        {
            this.OnCloseViewModel.ExecuteMethod(this,null);
            this.Dispose();
        }

        
        //public FunctionPageViewModel(string url):base()
        //{
        //    this.Url = url;
        //}


        public void Navigate(string url)
        {
            try
            {
                this.View.ShowWindowView();
                this.Url = url;
            }
            catch
            { }
        }

        public DelegateCommand CloseCommand { get; set; }
        public event EventHandler OnCloseViewModel;
    }
}
